<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2018/11/18
 * Time: 21:40
 */

namespace app\storage\controller;


use app\storage\model\PurchaseDetailModel;
use app\storage\model\PurchaseModel;
use app\storage\model\ReelsModel;
use app\storage\service\PurchaseService;
use cmf\controller\AdminBaseController;
use think\Db;
use think\Request;

load_trait('/util/Common');

class PurchaseController extends AdminBaseController
{

    private $purchaseModel;
    private $purchaseService;
    use \app\storage\util\Common;

    public function __construct(PurchaseModel $purchaseModel, PurchaseService $purchaseService, Request $request = null)
    {
        parent::__construct($request);
        $this->purchaseModel = $purchaseModel;
        $this->purchaseService = $purchaseService;
    }

    /**
     * 显示 采购单主表信息 模板
     */
    public function index()
    {
        $assign = $this->getAssignStorage();
        $this->assign($assign);
        return $this->fetch('index');
    }

    /**
     * 调用导入采购单添加视图
     */
    public function add()
    {
        return $this->fetch('add');
    }

    /**
     * excel导入视图
     */
    public function excel()
    {
        return $this->fetch('excel');
    }

    /**
     * 手动输入视图
     */
    public function manual()
    {
        return $this->fetch('manual');
    }

    /**
     * 返回 采购单主表信息 数据
     */
    public function getPurchase()
    {
        $where = $this->getWhere();
        $result = $this->purchaseModel->getPurchase($where);
        return json($result);
    }

    /**
     * 返回 采购单明细表信息 数据
     */
    public function getPurchaseDetail()
    {
        $where['where']['purchase_id'] = input('purchaseId');
        $purchaseDetail = new PurchaseDetailModel();
        $result = $purchaseDetail->getPurchaseDetail($where);
        return json($result);
    }


    /**
     * 删除采购单
     */
    public function deletePurchase()
    {
        $purchaseId = input('purchaseId');
        $reelId = Db::table('iwh_purchase_detail')->where('purchase_id', $purchaseId)->column('reel_id');
        if (!empty($reelId)) {
            $reelModel = new ReelsModel();
            $put = $reelModel->whereIn('reel_id', $reelId)->select();
            if (count($put)) {
                ajax_response(false, '该入库单已有物料上架请先出库后删除');
            }
        }
        Db::startTrans();
        $tranPur = Db::table('iwh_purchase')->where('purchase_id', $purchaseId)->delete();
        $tranDet = Db::table('iwh_purchase_detail')->where('purchase_id', $purchaseId)->delete();
        if ($tranPur == true && $tranDet !== false) {
            Db::commit();
            ajax_response(true, '删除成功');
        } else {
            Db::rollback();
            ajax_response(false, '删除异常');
        }
    }

    public function addPurchaseFromK3()
    {
        $purchaseId = input('purchaseId');
        $result =  $this->purchaseService->addPurchaseFromK3($purchaseId);
        return json($result);
    }


    /**
     * 读取excel
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */

    public function addPurchaseFromExcel()
    {
        $result = $this->purchaseService->addPurchaseFromExcel();
        return json($result);
    }

}